/**
 * create by ck on 2016/12/20.
 */
var resPackId;
var appGroupIds;
var inList;
var outList;
var inListParams;
var outListParams;
$(document).ready(function() {
    resPackId  = xhTool.getQuery('iRPId');
    var DataGrid2 = new DataGridEvent();
    var schoolId, schoolName;
    inList = $('#InList');
    outList = $('#OutList');
    inListParams = {"iRPId": resPackId,"type": "in"};
    outListParams = {"iRPId": resPackId,"type": "out"};
    inList.initDataGrid(baseUrl + 'Res/getPackContactGroupData', {"queryParams": inListParams})
        .setSelectThenEnable(['bt_out'], [], 'bs');
    outList.initDataGrid(baseUrl + 'Res/getPackContactGroupData', {"queryParams": outListParams}, DataGrid2)
        .setSelectThenEnable(['bt_in'], [], 'bs', DataGrid2);
    $('#bt_in').click(function(){
        appGroupIds = outList.getDataGridSelectedFields('iSAGId');
        contactQuestion('in');
    });
    $('#bt_out').click(function(){
        appGroupIds = inList.getDataGridSelectedFields('iSAGId');
        contactQuestion('out');
    });
    //学校下拉框
    $('#sl_School').select2({
        placeholder: "选择学校",
        allowClear: true
    }).on('change', function () {
        schoolId = $(this).val();
        schoolName = $(this).find("option:selected").text();
        inListParams.iSchoolId = schoolId;
        outListParams.iSchoolId = schoolId;
        inList.addDataGridParams(inListParams);
        outList.addDataGridParams(outListParams);
        if(schoolId){
            inList.datagrid('getPanel').panel('setTitle', '已关联应用组（' + schoolName + '）');
            outList.datagrid('getPanel').panel('setTitle', '未关联应用组（' + schoolName + '）');
        }
        else{
            inList.datagrid('getPanel').panel('setTitle', '已关联应用组');
            outList.datagrid('getPanel').panel('setTitle', '未关联应用组');
        }
    });
});
//根据类型进行关联或取消关联操作
function contactQuestion(type) {
    $.post(baseUrl + 'Res/setResPackPower', {iRPId: resPackId, values: appGroupIds, target: 'appGroup', type: type}, function (result) {
        $('body').append(result);
        inList.dataGridReload();
        outList.dataGridReload();
        $('#bt_in, #bt_out').setButtonDisable('bs');
    });
}